package com.example.caslistview.view;

import java.util.List;

import com.example.caslistview.R;
import com.example.caslistview.utils.Logger;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class CasListView extends ListView {

	public CasListView(Context context) {
		super(context);
		initView();
	}

	public CasListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView();
	}

	public CasListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initView();
	}

	private void initView() {
		mInflater = LayoutInflater.from(getContext());
	}

	/**
	 * 设置Adapter
	 * 
	 * @param headerList
	 *            header的列表
	 */
	public void setAdapter(List<View> headerList, List<Object> list1,
			List<Object> list2) {

		this.headerList = headerList;
		this.list1 = list1;
		this.list2 = list2;

		LinkedAdapter adapter = new LinkedAdapter();
		setAdapter(adapter);
	}

	private List<View> headerList;
	private List<Object> list1, list2;
	private LayoutInflater mInflater;

	private class LinkedAdapter extends BaseAdapter {

		private static final String TAG = "LinkedAdapter";

		@Override
		public int getCount() {
			int count = headerList.size() + list1.size() + list2.size();
			return count;
		}

		@Override
		public Object getItem(int position) {
			Object result = null;

			if (position == 0) {
				result = headerList.get(0);
			} else if (position > 0 && position < list1.size() + 1) {
				result = list1.get(position - 1);
			} else if (position == list1.size() + 1) {
				result = headerList.get(1);
			} else if (position > list1.size() + 1 && position < getCount()) {
				result = list2.get(position - 2 - list2.size());
			}

			return result;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (position == 0) {
				return (View) getItem(position);
			} else if (position > 0 && position < list1.size() + 1) {
				ViewHolder1 holder1 = null;
				if (convertView == null) {
					convertView = mInflater.inflate(R.layout.item1, null);
					holder1 = new ViewHolder1();
					holder1.tvItem1 = (TextView) convertView
							.findViewById(R.id.tv_item1);
					convertView.setTag(holder1);
				} else {
					Object tempHolder = convertView.getTag();
					if (tempHolder instanceof ViewHolder1) {
						Logger.e(TAG, "is holder1");
						holder1 = (ViewHolder1) tempHolder;
					} else {
						Logger.e(TAG, "is not holder1");
						convertView = mInflater.inflate(R.layout.item1, null);
						holder1 = new ViewHolder1();
						holder1.tvItem1 = (TextView) convertView
								.findViewById(R.id.tv_item1);
						convertView.setTag(holder1);
					}
				}
				String str = (String) getItem(position);
				holder1.tvItem1.setText(str);
			} else if (position == list1.size() + 1) {
				return (View) getItem(position);
			} else if (position > list1.size() + 1 && position < getCount()) {
				ViewHolder2 holder2 = null;
				if (convertView == null) {
					convertView = mInflater.inflate(R.layout.item2, null);
					holder2 = new ViewHolder2();
					holder2.btnItem2 = (Button) convertView
							.findViewById(R.id.btn_item2);
					convertView.setTag(holder2);
				} else {
					Object tempHolder = convertView.getTag();
					if (tempHolder instanceof ViewHolder2) {
						Logger.e(TAG, "is holder2");
						holder2 = (ViewHolder2) tempHolder;
					} else {
						Logger.e(TAG, "is not holder2");
						convertView = mInflater.inflate(R.layout.item2, null);
						holder2 = new ViewHolder2();
						holder2.btnItem2 = (Button) convertView
								.findViewById(R.id.btn_item2);
						convertView.setTag(holder2);
					}
				}
				String str = (String) getItem(position);
				holder2.btnItem2.setText(str);
			}
			
			return convertView;
		}
	}

	private class ViewHolder1 {
		private TextView tvItem1;
	}

	private class ViewHolder2 {
		private Button btnItem2;
	}
}
